package com.yango.order.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yango.commonutil.result.Result;
import com.yango.order.entity.PayLog;
import com.yango.order.service.PayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-08-04
 */
@Api(description = "支付日志表 前端控制器")
@RestController
@RequestMapping("/orderService/payLog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    //根据订单号生成微信支付二维码
    @ApiOperation(value = "根据订单号生成微信支付二维码")
    @GetMapping("/createPayCode/{orderNo}")
    public Result createPayCode(@ApiParam(name = "orderNo", value = "订单号", required = true)
                                @PathVariable String orderNo) {
        Map map = payLogService.createPayCode(orderNo);
        return Result.ok().data(map);
    }

    //查询订单支付状态
    //参数：订单号，根据订单号查询 支付状态
    @ApiOperation(value = "查询订单支付状态")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@ApiParam(name = "orderNo", value = "订单号", required = true)
                                 @PathVariable String orderNo) {
        //查询支付状态
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        //判断map是否为空
        if (StringUtils.isEmpty(map)) {
            return Result.error().message("支付出错了!");
        }
        //如果返回map不为空，根据map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")) {//支付成功
            //添加支付记录到支付记录表，更新订单支付状态
            payLogService.updateOrderStatus(map);
            return Result.ok().message("支付成功!");
        }
        return Result.ok().code(25000).message("支付中...");
    }

    //分页查询日志列表
    @ApiOperation(value = "分页查询日志列表")
    @GetMapping("/pageLogList/{current}/{limit}")
    public Result pageLogList(@ApiParam(name = "current", value = "当前页", required = true)
                              @PathVariable long current,
                              @ApiParam(name = "limit", value = "每页显示记录数", required = true)
                              @PathVariable long limit) {
        //创建page对象
        Page<PayLog> page = new Page<>();
        //查询
        payLogService.page(page, null);
        //返回
        return Result.ok().data("total", page.getTotal()).data("items", page.getRecords());
    }

    //根据id查询
    @ApiOperation(value = "根据id查询")
    @GetMapping("/getById/{id}")
    public Result getById(@ApiParam(name = "id", value = "日志id", required = true)
                          @PathVariable String id) {
        return Result.ok().data("paylog", payLogService.getById(id));
    }

    //删除
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@ApiParam(name = "id", value = "日志id", required = true)
                             @PathVariable String id) {
        boolean remove = payLogService.removeById(id);
        if (remove) {
            return Result.ok().message("删除日志成功!");
        } else {
            return Result.error().message("删除日志失败!");
        }
    }


}

